﻿using System;
using Epm.AppLogic.Framework;
using msBsc.Xsd.DataTds;
using System.Data;

namespace msBsc.AppLogic.DataAlc
{
    public class CBizCustomerEvlution : CustBizObject
    {
        private CDmDjkCustomerEvlution m_CDmDjkCustomerEvlution = new CDmDjkCustomerEvlution();
        private CDmDjkCustomerEvlution oDmDjkCustomerEvlution
        {
            get { return m_CDmDjkCustomerEvlution; }
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        public CBizCustomerEvlution()
            : base()
        {
            this.DataObj = new CDbAccess();
            this.DataObj.Init(this.m_CDmDjkCustomerEvlution.DJK_CUSTOMER_EVLUTION);
            this.TableName = this.DataObj.TableName;
            this.DataObj.KeyField = this.DataObj.Fields["CUSTOMER_EVLUTION_ID"];
        }

        /// <summary>
        /// 新增记录前的处理
        /// </summary>
        /// <param name="drw">新增数据行</param>
        /// <returns>true/false</returns>
        /// <remarks>
        /// 生成ID值
        /// </remarks>
        protected override bool BeforeRowAdded(DataRow drw)
        {
            if (drw[this.KeyFieldName].ToString().Length < 10)
            {
                drw[this.KeyFieldName] = this.DataObj.NewID();
            }
            return true;
        }

        /// <summary>
        /// 记录修改前检验
        /// </summary>
        /// <param name="drw">当前记录</param>
        /// <returns>>校验合法，返回值为true,否则返回false</returns>
        /// <remarks>
        /// ID不允许修改,并把此修改操作记录到BSE_MODIFY_HISTORY表中
        /// </remarks>
        protected override bool BeforeRowModified(DataRow drw)
        {
            //ID不容许修改
            if (drw[this.KeyFieldName].ToString() != drw[this.KeyFieldName, DataRowVersion.Original].ToString())
            {
                Exception e = new Exception(this.TableName + "表的主键" + this.KeyFieldName + "字段不允许被修改");
                throw e;
            }
            return true;
        }
        protected override bool BeforeRowDeletedR(DataRow drw)
        {
            //using (msBsc.AppLogic.DataAlc.CBizShpPageColumn objtem = new CBizShpPageColumn())
            //{
            //    DataSet ds = objtem.SelectByPageNo(this.GetRowStr(drw, KeyFieldName));
            //    for (int i = ds.Tables[0].Rows.Count - 1; i >= 0; i--)
            //    {
            //        ds.Tables[0].Rows[i].Delete();
            //    }
            //    objtem.Update(ds);
            //} 
            return true;
        }

        protected override bool AfterRowAdded(DataRow drw)
        {
            return true;
        }

        protected override bool AfterRowModified(DataRow drw)
        {
            return true;
        }

        protected override bool AfterRowDeletedR(DataRow drw)
        {
            return true;
        }
        /// <summary>
        /// 添加评论信息
        /// </summary>
        /// <param name="ordesInfoId">订单ID</param>
        /// <param name="productID">产品（车辆）ID</param>
        /// <param name="comments">评论内容</param>
        /// <param name="isImage">评论是否有图片 默认false</param>
        /// <returns></returns>
        public string AddCustomerEvlution(string ordesInfoId, string productID, string comments, string[] images)
        {
            string userId = msBsc.SvRef.UserInfo.Operator;
            if (string.IsNullOrEmpty(userId)) { return "用户ID不能为空"; }
            if (string.IsNullOrEmpty(ordesInfoId)) { return "订单ID不能为空"; }
            if (string.IsNullOrEmpty(productID)) { return "车辆ID 不能为空"; }
            if (string.IsNullOrEmpty(userId)) { return "评论内容不能为空"; }

            //该车辆 是否在该订单
            using (CBizDJkOrdersDetail detail = new CBizDJkOrdersDetail())
            {
                if (!detail.OrdersWhetherThere(ordesInfoId, productID)) { return "车辆不属于该订单"; }

            }
            //判断交易状态 
            using (CBizOrdersInfo orderInfo = new CBizOrdersInfo())
            {
                if (orderInfo.GetOrderStatus(ordesInfoId, userId) != OrderStatusEnum.Finished) { return "交易完成的订单才可以评论"; }
            }
            //每单每车可评论1次
            CDbParmCollection parmCollection = new CDbParmCollection();
            parmCollection.Add(new CDbParm("ProductID", ParameterDirection.Input, EDbType.String, 36, productID));
            parmCollection.Add(new CDbParm("OrdersID", ParameterDirection.Input, EDbType.String, 36, ordesInfoId));
            DataSet ds = this.DataObj.QueryBySQL(@"SELECT CUSTOMER_EVLUTION_ID FROM DJK_CUSTOMER_EVLUTION WHERE EVLUTION_ORDER_ID=@OrdersID AND  EVLUTION_VEHICLE_ID=@ProductID", parmCollection);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return "该单已评论，每单每车只可评论1次";
            }

            //评论 和 评论图片 添加
            System.Transactions.TransactionOptions transactionOption = new System.Transactions.TransactionOptions();
            transactionOption.IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted;
            transactionOption.Timeout = new TimeSpan(0, 10, 0);
            using (System.Transactions.TransactionScope ts = new System.Transactions.TransactionScope(System.Transactions.TransactionScopeOption.Required, transactionOption))
            {
                DataSet dsComments = new CDmDjkCustomerEvlution();

                dsComments.Tables[0].TableName = this.TableName;
                DataRow dr = dsComments.Tables[0].NewRow();
                dr["CUSTOMER_EVLUTION_ID"] = this.DataObj.NewID();
                dr["EVLUTION_CUSTOMER_ID"] = userId;
                dr["EVLUTION_ORDER_ID"] = ordesInfoId;
                dr["EVLUTION_CONTENT"] = comments;
                dr["EVLUTION_TIME"] = this.dtCurrDateTime;
                dr["EVLUTION_HAS_PIC"] = images.Length > 0;
                dr["EVLUTION_IP"] = msBsc.Cls.CommValiCollection.GetIp();
                dr["EVLUTION_VEHICLE_ID"] = productID;
                dr["EVLUTION_IS_DELETED"] = 0;
                dr["EVLUTION_IS_DISPLAY"] = 1;

                dsComments.Tables[0].Rows.Add(dr);

                //添加图片信息
                using (msBsc.AppLogic.DataAlc.CBizEvlutionImages CBEComments = new AppLogic.DataAlc.CBizEvlutionImages())
                {
                    DataSet EvlutionImages = new CDmDjkEvlutionImages();
                    EvlutionImages.Tables[0].TableName = CBEComments.TableName;
                    string commentid = dr["CUSTOMER_EVLUTION_ID"].ToString();
                    for (int i = 0; i < images.Length; i++)
                    {
                        DataRow drow = EvlutionImages.Tables[0].NewRow();
                        drow["EVLUTION_IMAGES_ID"] = this.DataObj.NewID();
                        drow["EVLUTION_ID"] = commentid;
                        drow["EVLUTION_IMAGES_NAME"] = "";
                        drow["EVLUTION_IMAGES_SOURCE"] = images[i];
                        drow["EVLUTION_IMAGES_DEFAULT"] = i < 1;
                        drow["IMAGES_IS_DELETED"] = 0;
                        drow["IMAGES_IS_DISPLAY"] = 1;
                        EvlutionImages.Tables[0].Rows.Add(drow);
                    }
                    CBEComments.Update(EvlutionImages);
                }

                //修改 评论状态
                using (CBizDJkOrdersDetail detail = new CBizDJkOrdersDetail())
                {
                    DataSet dsDetail = detail.GetDetailInfo(ordesInfoId,productID);
                    dsDetail.Tables[0].Rows[0]["IS_COMMENT"] = 1;
                    detail.Update(dsDetail);
                }

                this.Update(dsComments);
                ts.Complete();
            }

            return "OK";
        }
        /// <summary>
        /// 获取 车辆评论信息
        /// </summary>
        /// <param name="pageRows">每页多少条数据</param>
        /// <param name="pageNum">第几页</param>
        /// <param name="vehicleid">车辆编号ID</param>
        /// <returns></returns>
        public DataSet GetCommentsInfo(int pageRows, int pageNum, string vehicleid)
        {
            CDbParmCollection parmCollection = new CDbParmCollection();
            parmCollection.Add(new CDbParm("ipagerows", ParameterDirection.Input, EDbType.String, 36, pageRows));
            parmCollection.Add(new CDbParm("ipagenum", ParameterDirection.Input, EDbType.String, 36, pageNum));
            parmCollection.Add(new CDbParm("vehicleid", ParameterDirection.Input, EDbType.String, 36, vehicleid));
            DataSet ds = this.DataObj.QueryBySP("DJK_SELECT_DJK_CUSTOMER_EVLUTION_VIEW", parmCollection);
            return ds;
        }
    }
}
